/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-information
*
* @File name : InformationController.java
*
* @Author : Administrator
*
* @Date : 2017年3月10日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年3月10日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.information.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.dto.information.ActivityInfoDto;
import org.marketing.common.dto.information.ActivityItemsDto;
import org.marketing.common.dto.information.ActivityRecordDto;
import org.marketing.common.dto.information.ReadingDto;
import org.marketing.common.dto.leads.ThreadcarDto;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.information.constant.InformationConstant;
import com.yonyou.gmmc.service.information.service.InformationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 * @author Administrator TODO description
 * @date 2017年3月10日
 */
@Controller
@RestController
@RequestMapping(value = InformationConstant.BASE_PATH + "/" + InformationConstant.VERSION)
@Api("资讯")
public class InformationController {

	private Logger logger = Logger.getLogger(InformationController.class);
	@Autowired
	private InformationService informationService;

	/**
	 * 获取新闻资讯内容
	 * 
	 * @author QianKun
	 * @date 2017年3月10日
	 * @param naiId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sharepage/newsContent", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取资讯/活动内容", notes = "获取资讯/活动内容")
	@ResponseBody
	public Map<String, Object> getNewsActivityContent(@RequestParam("naiId") Integer naiId,
			@RequestParam("type") Integer type) {
		Map<String, Object> resultMap = new HashMap<>();

		String content = informationService.getNewsActivityContent(naiId, type);
		String remark = informationService.getInforNews(naiId, type);
		resultMap.put("content", content);
		resultMap.put("remark", remark);
		return resultMap;
	}

	/**
	 * 保存留资信息
	 * 
	 * @author QianKun
	 * @date 2017年3月10日
	 * @param capitalInformationDto
	 * @return
	 */
	@RequestMapping(value = "/sharepage/saveCapitalInfor", method = RequestMethod.POST)
	@ApiOperation(value = "保存留资信息", notes = "保存留资信息")
	@ResponseBody
	public Map<String, Object> saveCapitalInfor(@RequestBody ThreadcarDto threadcarDto) {
		Map<String, Object> map = new HashMap<>();
		try {
			map = informationService.saveCapitalInfor(threadcarDto);
		} catch (Exception e) {
			logger.error("发生异常: ", e);
			map.put("errorCode", "1");
			map.put("errorMsg", "您已提交请求，如需更新，请1小时候后再尝试!");
		}
		return map;
	}

	/**
	 * 活动填写项目查询
	 * 
	 * @author QianKun
	 * @date 2017年3月10日
	 * @param naiId
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/sharepage/activityItems", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "活动填写项目查询", notes = "活动填写项目查询")
	public List<ActivityItemsDto> getActivityItems(@RequestParam("naiId") Long naiId) {
		List<ActivityItemsDto> dtoList = informationService.getActivityItems(naiId);
		return dtoList;
	}

	/**
	 * 新增报名人信息
	 * 
	 * @author QianKun
	 * @date 2017年3月11日
	 * @param activityRecordDto
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/sharepage/insertPotentialUser", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增活动报名", notes = "新增报名人信息")
	public Map<String, Object> insertPotentialUser(@RequestBody ActivityRecordDto activityRecordDto) {
		Map<String, Object> map = new HashMap<>();
		try {
			map = informationService.insertPotentialUser(activityRecordDto);
		} catch (Exception e) {
			logger.error("发生异常: ", e);
			map.put("errorCode", "1");
			map.put("errorMsg", "您已提交请求，如需更新，请1小时候后再尝试!");
		}
		return map;
	}

	/**
	 * 统计阅读数
	 * 
	 * @author QianKun
	 * @date 2017年3月13日
	 * @param readingDto
	 * @return
	 */
	@RequestMapping(value = "/sharepage/readingNum", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "统计阅读量", notes = "统计阅读量")
	public Map<String, Object> saveReadingNum(@RequestBody ReadingDto readingDto) {
		Map<String, Object> map = informationService.saveReadingNum(readingDto);
		return map;
	}

	@RequestMapping(value = "/sharepage/sendTicket", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "分享成功自动发放卡券", notes = "分享成功自动发放卡券")
	public Map<String, Object> sendTicket(@RequestBody ReadingDto readingDto) {
		Map<String, Object> map = informationService.sendTicket(readingDto);
		return map;
	}

	/**
	 * 已报名信息列表查询
	 * 
	 * @author QianKun
	 * @date 2017年3月15日
	 * @param userId_
	 * @return
	 */
	@RequestMapping(value = "/activityLists", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "已报名列表查询", notes = "已报名列表查询")
	public List<ActivityRecordDto> getActivityList(@RequestParam("userId_") String userId_,
			HttpServletRequest request) {
		logger.info("--已报名列表查询--");
		String userId = "";
		if (!CheckUtil.NullOrEmpty(userId_)) {
			userId = userId_;
		} else {
			userId = request.getHeader(CommonConstant.HEAD_USER_ID);
		}
		List<ActivityRecordDto> dtoList = informationService.getActivityList(userId);
		return dtoList;
	}

	/**
	 * 已报名信息详情查询
	 * 
	 * @author QianKun
	 * @date 2017年3月15日
	 * @param applyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/activityInfo", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "已报名详情查询", notes = "已报名详情查询")
	public List<ActivityRecordDto> getActivityInfo(@RequestParam("applyId") Long applyId) {
		List<ActivityRecordDto> dtoList = informationService.getActivityInfo(applyId);
		return dtoList;
	}

	/**
	 * 我能看到的资讯查询
	 * 
	 * @author QianKun
	 * @date 2017年3月16日
	 * @param openId
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/myNews", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "我能看到的资讯查询 ", notes = "我能看到的资讯查询 ")
	public List<ActivityInfoDto> getMyNews(@RequestParam(value = "userId_", required = false) String userId_,
			@RequestParam(value = "activityType", required = false) Long activityType, HttpServletRequest request) {
		logger.info("--我能看到的资讯查询--");
		String userId = "";
		if (!CheckUtil.NullOrEmpty(userId_)) {
			userId = userId_;
		} else {
			userId = request.getHeader(CommonConstant.HEAD_USER_ID);
		}
		String ip = getRemoteHost(request);
		logger.info("获取到的IP为：" + ip);
		List<ActivityInfoDto> dtoList = informationService.getMyNews(userId, activityType);
		return dtoList;
	}

	public String getRemoteHost(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}

	/**
	 * 我能看到的活动查询
	 * 
	 * @author QianKun
	 * @date 2017年3月16日
	 * @param userId_
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/myActivities", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "我能看到的活动查询 ", notes = "我能看到的活动查询 ")
	public List<ActivityInfoDto> getMyActivities(@RequestParam(value = "userId_", required = false) String userId_,
			HttpServletRequest request) {
		logger.info("--我能看到的活动查询--");
		String userId = "";
		if (!CheckUtil.NullOrEmpty(userId_)) {
			userId = userId_;
		} else {
			userId = request.getHeader(CommonConstant.HEAD_USER_ID);
		}
		List<ActivityInfoDto> dtoList = informationService.getMyActivities(userId);
		return dtoList;
	}

	/**
	 * 检查商城扣分
	 * 
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "/checkShoppingPoints", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "检查商城扣分 ", notes = "检查商城扣分 ")
	public String checkShoppingPoints() {
		try {
			String result = informationService.checkShoppingPoints();
			return result;
		} catch (Exception e) {
			logger.error("发生异常: ", e);
			return "发生异常";
		}
	}
}
